<?php

namespace think\wework\Api;

use GuzzleHttp\Exception\GuzzleException;
use think\wework\Abstracts\WeworkApiWithToken;

class GroupChat extends WeworkApiWithToken
{
    private static ?GroupChat $instance = null;

    public static function getInstance(): GroupChat
    {
        if (!self::$instance instanceof GroupChat) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * 获取客户群列表
     * @param array $userIds
     * @param int $statusFilter
     * @return array
     */
    public function list(array $userIds, int $statusFilter = 0): array
    {
        $json = [
            'status_filter'=>$statusFilter,
            'owner_filter'=>[
                'userid_list'=>$userIds
            ],
            'limit'=>1000
        ];
        return $this->httpClient->post('externalcontact/groupchat/list', compact('json'))->toArray();
    }

    /**
     * 获取外部联系人详情
     * @param string $chatId
     * @param bool $needName
     * @return array
     */
    public function get(string $chatId, bool $needName = true): array
    {
        $json = [
            'chat_id' => $chatId,
            'need_name' => $needName ? 1 : 0,
        ];
        return $this->httpClient->post('externalcontact/groupchat/get', compact('json'))->toArray();
    }

    /**
     * 客户群「加入群聊」
     * @param array $chat_id_list 社群ID数组
     * @param string $room_base_name 自动建群的群名前缀
     * @param bool $auto_create_room 当群满了后，是否自动新建群
     * @param int $room_base_id 自动建群的群起始序号
     * @param string $state 企业自定义的state参数，用于区分不同的添加渠道
     * @param string $remark 联系方式的备注信息，用于助记
     * @return array
     */
    public function addJoinWay(array $chat_id_list = [],string $room_base_name = '', bool $auto_create_room = true, int $room_base_id = 1, string $state = '', string $remark = ''): array
    {
        $json = [
            'scene' => 2,
            'remark' => $remark,
            'auto_create_room' => $auto_create_room ? 1 : 0,
            'room_base_name' => $room_base_name,
            'room_base_id' => $room_base_id,
            'chat_id_list' => $chat_id_list,
            'state' => $state
        ];

        return $this->httpClient->post('externalcontact/groupchat/add_join_way', compact('json'))->toArray();
    }

    /**
     * 获取客户群进群方式配置
     * @param string $configId
     * @return array
     */
    public function getJoinWay(string $configId): array
    {
        $json = [
            'config_id' => $configId
        ];
        return $this->httpClient->post('externalcontact/groupchat/get_join_way', compact('json'))->toArray();
    }
}
